package com.xiecong.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiecong.excel.StudentModel;
import com.xiecong.mapper.StudentMapper;
import com.xiecong.pojo.PageResult;
import com.xiecong.pojo.Student;
import com.xiecong.pojo.StudentQueryParam;
import com.xiecong.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 分页查询学生列表
     */
    @Override
    public PageResult<Student> pageQuery(StudentQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        List<Student> students = studentMapper.pageQuery(studentQueryParam);
        Page<Student> pageinfo = (Page<Student>) students;
        return new PageResult<Student>(pageinfo.getTotal(),pageinfo.getResult());
    }


    /**
     * 添加学生
     */
    @Override
    public void add(Student student) {
        LocalDateTime now = LocalDateTime.now();
        student.setCreateTime(now);
        student.setUpdateTime(now);
        studentMapper.insert(student);
    }


    /**
     * 修改学生信息-数据回显-根据id查询
     */
    @Override
    public Student findById(Integer id) {
        Student student = studentMapper.findById(id);
        return student;
    }


    /**
     * 修改学生信息
     */
    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }


    /**
     * 删除学生信息（单条或批量）
     */
    @Override
    public void deleteByIds(List<Integer> ids) {
        studentMapper.deleteByIds(ids);
    }


    /**
     * 违纪处理
     */
    @Override
    public void violationHandle(Integer id, Integer score) {
        Student student = studentMapper.findById(id);
        Short violationCount = student.getViolationCount();
        Short violationScore = student.getViolationScore();
        if(score<=0  && (violationScore+score) <= 100){
            throw new RuntimeException("违纪分数不能小于0");
        }else if(score>0 && (violationScore+score) >= 100){
            throw new RuntimeException("违纪分数不能大于100");
        }else{
            student.setViolationScore((short) (violationScore + score));
            student.setViolationCount((short)(violationCount+1));
            student.setUpdateTime(LocalDateTime.now());
        }
        studentMapper.update(student);
    }

    @Override
    public void importStudent(List<StudentModel> studentModelList, Integer clazzId) {
        // 学历信息
        Map<String,Integer> degreeMap = new HashMap<>();
        degreeMap.put("初中",1);
        degreeMap.put("高中",2);
        degreeMap.put("大专",3);
        degreeMap.put("本科",4);
        degreeMap.put("硕士",5);
        degreeMap.put("博士",6);
        degreeMap.put("其他",7);
        List<Student> stuList = studentModelList.stream().map(sm -> {
            //把StudentModel转成Student对象
            Student student = new Student();
            student.setName(sm.getName());
            student.setNo(sm.getNo());
            student.setGender("男".equals(sm.getGenderStr()) ? 1 : 0);
            student.setPhone(sm.getPhone());
            student.setIdCard(sm.getIdCard());
            student.setIsCollege("是".equals(sm.getIsCollegeStr()) ? 1 : 0);
            student.setAddress(sm.getAddress());
            student.setDegree(degreeMap.get(sm.getDegreeStr()));
            student.setGraduationDate(sm.getGraduationDate());
            student.setClazzId(clazzId);
            student.setCreateTime(LocalDateTime.now());
            student.setUpdateTime(student.getCreateTime());
            return student;
        }).toList();

        studentMapper.insertBatch(stuList);
    }

}
